/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditPredicateBox.java
 *
 * Created on 10/10/2009, 17:34:50
 */
package gui;

import base.KnowledgeBaseManager;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;

import exceptions.UnDecidibleValueException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import logicaDePredicados.LogicPredicate;

/**
 *
 * @author Francisco Gindre
 */
public class EditPredicateBox extends javax.swing.JFrame {

    private LogicPredicate<String> targetPredicate;
    private File baseFile;
    private ObjectContainer db = null;
private ObjectSet<LogicPredicate<String>> resultSet;
    /** Creates new form EditPredicateBox */
    public EditPredicateBox(LogicPredicate<?> toEdit, File baseFile) {
        WindowListener windowListener = new WindowAdapter() {
            // anonymous WindowAdapter class

            @Override
            public void windowClosing(WindowEvent w) {
                // Whatever application code you want to do on close, e.g.
                if (db != null) {
             
            db.rollback();
            db.close();
            w.getWindow().dispose();
        } else {


        }

                // Whatever code you want to actually close the JFrame, e.g.

            } // end windowClosing
        };// end anonymous class
        this.addWindowListener(windowListener);

        try {
            db = Db4oEmbedded.openFile(KnowledgeBaseManager.getConfiguration(), baseFile.getAbsolutePath());
             resultSet = db.queryByExample(toEdit);
            if (resultSet.size() > 1) {
                new ErrorFrame("Duplicate Predicate on knowledge Base");
                setVisible(true);
                  this.processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
            } else {
                targetPredicate = resultSet.next();
            }

        } catch (Exception e) {
                ErrorFrame error = new ErrorFrame("Database error: could not retrieve Object \n please refresh db and try again");
                error.setVisible(true);
                this.processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
            
        }




        initComponents();
        predicateNameTextField.setText(toEdit.getName());

        setTitle("Edit: " + targetPredicate.getName());
        


    }

    public EditPredicateBox() {


        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        valueTextField = new javax.swing.JTextField();
        changeButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        booleanCombo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        predicateNameTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        booleanCombo1 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        keyComboBox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/resources/icons/background.png"))); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel1.setText("Key");
        jLabel1.setName("jLabel1"); // NOI18N

        valueTextField.setName("valueTextField"); // NOI18N
        valueTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                valueTextFieldActionPerformed(evt);
            }
        });

        changeButton.setText("Change");
        changeButton.setName("changeButton"); // NOI18N
        changeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        saveButton.setText("Save");
        saveButton.setName("saveButton"); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        booleanCombo.setModel(new javax.swing.DefaultComboBoxModel(new Boolean[] { new Boolean(false), new Boolean(true)}));
        booleanCombo.setName("booleanCombo"); // NOI18N
        booleanCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                booleanComboActionPerformed(evt);
            }
        });

        jLabel2.setText("Add new key value");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Boolean Value");
        jLabel3.setName("jLabel3"); // NOI18N

        predicateNameTextField.setName("predicateNameTextField"); // NOI18N
        predicateNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                predicateNameTextFieldActionPerformed(evt);
            }
        });

        jLabel5.setText("Predicate Name");
        jLabel5.setName("jLabel5"); // NOI18N

        booleanCombo1.setModel(new javax.swing.DefaultComboBoxModel(new Boolean[] { new Boolean(false), new Boolean(true)}));
        booleanCombo1.setName("booleanCombo1"); // NOI18N
        booleanCombo1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                booleanCombo1ActionPerformed(evt);
            }
        });

        jLabel6.setText("Boolean Value");
        jLabel6.setName("jLabel6"); // NOI18N

        addButton.setText("Add");
        addButton.setName("addButton"); // NOI18N
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        removeButton.setText("Remove");
        removeButton.setName("removeButton"); // NOI18N
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        keyComboBox.setModel(new javax.swing.DefaultComboBoxModel(targetPredicate.getKeyList().toArray(new String[0])));
        keyComboBox.setName("keyComboBox"); // NOI18N
        keyComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keyComboBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(341, 341, 341)
                .addComponent(saveButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cancelButton)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(predicateNameTextField)
                            .addComponent(jLabel5)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(valueTextField)
                            .addComponent(jLabel1)
                            .addComponent(keyComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel3)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(booleanCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(booleanCombo1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(26, 26, 26)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(addButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(changeButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(removeButton)))
                        .addGap(43, 43, 43))
                    .addComponent(jLabel4))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addGap(3, 3, 3)
                .addComponent(predicateNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(booleanCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(changeButton)
                    .addComponent(removeButton)
                    .addComponent(keyComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(valueTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(booleanCombo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(saveButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void valueTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_valueTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_valueTextFieldActionPerformed

    private void changeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeButtonActionPerformed
        targetPredicate.addTruthValue((String) keyComboBox.getSelectedItem(), (Boolean) booleanCombo.getSelectedItem());

    }//GEN-LAST:event_changeButtonActionPerformed

    private void booleanComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_booleanComboActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_booleanComboActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
       this.processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void predicateNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_predicateNameTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_predicateNameTextFieldActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        // TODO add your handling code here:
        targetPredicate.setName(predicateNameTextField.getText());
        if (db != null) {
            db.store(targetPredicate);
            db.commit();
            db.close();
            this.dispose();
            
        } else {
            new ErrorFrame("Database Error").setVisible(true);
            
        }
       

    }//GEN-LAST:event_saveButtonActionPerformed

    private void booleanCombo1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_booleanCombo1ActionPerformed
        // TODO add your handling code here:

    }//GEN-LAST:event_booleanCombo1ActionPerformed

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        targetPredicate.removeKey((String) keyComboBox.getSelectedItem());
        refresh();
    }//GEN-LAST:event_removeButtonActionPerformed

    private void keyComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keyComboBoxActionPerformed
        // TODO add your handling code here:
        String selectedOption = (String) keyComboBox.getSelectedItem();
        try {
            if (targetPredicate.getTruthValue(selectedOption)) {
                booleanCombo.setSelectedIndex(1);
            } else {
                booleanCombo.setSelectedIndex(0);
            }
        } catch (UnDecidibleValueException ex) {
            ErrorFrame error = new ErrorFrame (ex.getMessage());
            error.setVisible(true);
            
        }

    }//GEN-LAST:event_keyComboBoxActionPerformed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        targetPredicate.addTruthValue(valueTextField.getText(), (Boolean)booleanCombo1.getSelectedItem());
        valueTextField.setText(null);
        
        refresh();

    }//GEN-LAST:event_addButtonActionPerformed

    private void refresh() {
        keyComboBox.setModel(new javax.swing.DefaultComboBoxModel(targetPredicate.getKeyList().toArray(new String[0])));
        
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new EditPredicateBox().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JComboBox booleanCombo;
    private javax.swing.JComboBox booleanCombo1;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton changeButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JComboBox keyComboBox;
    private javax.swing.JTextField predicateNameTextField;
    private javax.swing.JButton removeButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextField valueTextField;
    // End of variables declaration//GEN-END:variables
}
